const CryptoJS = require('crypto-js');  //引用AES源码js


//加密方法
function Encrypt(PassWord,key,iv,salt) {
    let secKey = CryptoJS.PBKDF2(key, salt, {
        keySize: 256 / 32,
        iterations: 1
    });
    let ciphertext = CryptoJS.AES.encrypt(PassWord, secKey, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }).toString();
    return ciphertext
}

//解密方法
function Decrypt(word,key,iv) {
    let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}

// 本地使用 加密 utils.SelfUseEncrypt(JSON.stringify(September))
var SelfUseEncrypt = function(word){
    if(!word){
        console.log('加密值为空');
        return '{}'
    }
    var key  = CryptoJS.enc.Utf8.parse('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
    var iv  = CryptoJS.enc.Utf8.parse('1234567812345678')
    return CryptoJS.AES.encrypt(word, CryptoJS.enc.Utf8.parse(key), {
        iv: CryptoJS.enc.Utf8.parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }).toString()
}

// 解密   JSON.parse(utils.SelfUseDecrypt(September))
var SelfUseDecrypt = function(word){
    if(!word){
        console.log('解密值为空');
        return '{}'
    }
    var key  = CryptoJS.enc.Utf8.parse('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
    var iv  = CryptoJS.enc.Utf8.parse('1234567812345678')
    let decrypted = CryptoJS.AES.decrypt(word, CryptoJS.enc.Utf8.parse(key), {
        iv: CryptoJS.enc.Utf8.parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    })
    return decrypted.toString(CryptoJS.enc.Utf8)
}



export default {
    // 密码加密
    Decrypt,
    Encrypt,
    CryptoJS,
    // 本地数据加密
    SelfUseEncrypt,
    SelfUseDecrypt,
}
